package com.xuan.web;
import com.alibaba.druid.util.StringUtils;
import com.xuan.dao.GameDao;
import com.xuan.model.Game;
import com.xuan.service.OrderService;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/servlet/OrdersServiceAction")
@MultipartConfig(maxFileSize = 10*1024*1024)
@SuppressWarnings("all")
public class OrdersServiceAction extends HttpServlet {
    private GameDao gameDao = new GameDao();
    private OrderService service = new OrderService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String act = request.getParameter("act");
        if (act == null) {
            act = "list";
        }
        switch (act) {
            default:
                int page = 1;
                String size = getServletContext().getInitParameter("pageSize");
                String page1 = request.getParameter("page");
                if (page1 != null) {
                    page = Integer.parseInt(page1);
                }
            request.setAttribute("page",page);
                request.setAttribute("lastpage",service.getPageSize(Integer.parseInt(size)));
                forward(request,response,"orders",
                        service.getAll(Integer.parseInt(size),page),"/WEB-INF/jsp/serviceOrder/title.jsp");
        }
    }

    private String checkServiceType(Game game) {
        String msg = "";
        if (StringUtils.isEmpty(game.getTitle())) {
            return "请输入游戏名";
        }
        return msg;
    }

    private void forward(HttpServletRequest request, HttpServletResponse response, String key,Object value, String url) throws ServletException, IOException {
        request.setAttribute(key, value);
        request.getRequestDispatcher(url).forward(request, response);
    }
    private void forward(HttpServletRequest request, HttpServletResponse response, String msg, String url) throws ServletException, IOException {
        request.setAttribute("msg", msg);
        request.getRequestDispatcher(url).forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
